package com.xiaoxi.dragsortlistview.countdown.bean.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.xiaoxi.dragsortlistview.R;
import com.xiaoxi.dragsortlistview.countdown.bean.CountBean;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 功能说明： </br>
 *
 * @author:
 * @version: 1.0
 * @date: 2016/11/3
 * @Copyright (c) 2016. Administrator Inc. All rights reserved.
 */

public class CountdownAdapter extends BaseAdapter {

    private Context mContext;
    private List<CountBean> mCountBeanList;


    public CountdownAdapter( Context context,List<CountBean> countBeanList) {
        mCountBeanList = countBeanList;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mCountBeanList == null ? 0 : mCountBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
         ViewHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_list_count_down, null);
            holder = new ViewHolder();
            holder.tv_date = (TextView) convertView.findViewById(R.id.item_count_list_text);
            holder.clickButton = (Button) convertView.findViewById(R.id.item_count_list_btn);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_date.setText("");
        holder.clickButton.setText(mCountBeanList.get(position).buttonContent);
        return convertView;
    }

    private static class ViewHolder {
        TextView tv_date;//倒计时
        Button clickButton;//
    }

    public void updateTime(View view, CountBean countBean){
        TextView tvDate = (TextView) view.findViewById(R.id.item_count_list_text);
        if(tvDate == null){
            return;
        }
        Date endDate = stringToDate(countBean.getEndTime(),"yyyy-MM-dd HH:mm:ss");
        long date = endDate.getTime() - new Date().getTime();
        if(date < 0){
            tvDate.setText("");
        }else{
            long day = date/(1000*60*60*24);
            long hour = (date / (60 * 60 * 1000) - day * 24);
            long min = ((date / (60 * 1000)) - day * 24 * 60 - hour * 60);
            long sec = (date/1000-day*24*60*60-hour*60*60-min*60);
            String strDate = day+"天 " + hour + "小时 " + min + "分钟 " + sec + "秒";
//            LogUtil.e(" date = " + strDate);
            tvDate.setText(strDate);

        }
    }

    /**
     * @param date
     * @param format
     * @return
     */
    public static Date stringToDate(String date, String format) {
        DateFormat format1 = new SimpleDateFormat(format);
        Date time = null;
        try {
            time = format1.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }
}
